package com.wntms.wgj.service.impl;

import com.github.pagehelper.PageHelper;
import com.wntms.wgj.dao.CollectionSettleDao;
import com.wntms.wgj.pojo.CollectionSettle;
import com.wntms.wgj.pojo.DriverSettle;
import com.wntms.wgj.service.CollectionSettleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
public class CollectionSettleServiceImpl implements CollectionSettleService {
    @Autowired
    private CollectionSettleDao collectionSettleDao;

    @Override
    public List<CollectionSettle> findAll() {
        List<CollectionSettle> list = collectionSettleDao.findAll();
        for (CollectionSettle collectionSettle : list) {
            if (collectionSettle.getFreightContracts().getRefundtype().equals(1)){
                Double commission = collectionSettle.getAfterReceivables() * 0.01;
                BigDecimal bigDecimal = new BigDecimal(commission);
                double numm = bigDecimal.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
                collectionSettle.setCommission(numm);
            }else if (collectionSettle.getFreightContracts().getRefundtype().equals(2)){
                Double commission = collectionSettle.getAfterReceivables() * 0.02;
                BigDecimal bigDecimal = new BigDecimal(commission);
                double numm = bigDecimal.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
                collectionSettle.setCommission(numm);
            }
        }
        return list;
    }

    @Override
    public List<CollectionSettle> findAll(Integer currentPage, Integer pageSize) {
        PageHelper.startPage(currentPage,pageSize);

        List<CollectionSettle> list = collectionSettleDao.findAll();
        for (CollectionSettle collectionSettle : list) {
            if (collectionSettle.getFreightContracts().getRefundtype().equals(1)){
                Double commission = collectionSettle.getAfterReceivables() * 0.01;
                BigDecimal bigDecimal = new BigDecimal(commission);
                double numm = bigDecimal.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
                collectionSettle.setCommission(numm);
            }else if (collectionSettle.getFreightContracts().getRefundtype().equals(2)){
                Double commission = collectionSettle.getAfterReceivables() * 0.02;
                BigDecimal bigDecimal = new BigDecimal(commission);
                double numm = bigDecimal.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
                collectionSettle.setCommission(numm);
            }
        }

        return list;
    }

    @Override
    public boolean updateSettle(CollectionSettle collectionSettle) {

        return collectionSettleDao.updateSettle(collectionSettle) == 1 ? true:false;
    }

    @Override
    public boolean deleteSettle(Integer id) {
        return collectionSettleDao.deleteSettle(id) == 1 ? true:false;
    }

    @Override
    public CollectionSettle findByFid(String fid) {
        CollectionSettle collectionSettle = collectionSettleDao.findByFid(fid);
        if (collectionSettle.getFreightContracts().getRefundtype().equals(1)){
            Double commission = collectionSettle.getAfterReceivables() * 0.01;
            BigDecimal bigDecimal = new BigDecimal(commission);
            double numm = bigDecimal.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
            collectionSettle.setCommission(numm);
        }else if (collectionSettle.getFreightContracts().getRefundtype().equals(2)){
            Double commission = collectionSettle.getAfterReceivables() * 0.02;
            BigDecimal bigDecimal = new BigDecimal(commission);
            double numm = bigDecimal.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
            collectionSettle.setCommission(numm);
        }

        return collectionSettle;
    }

}
